package com.quectel.core.module.scheduling.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.dept.dto.DeptPersonDto;
import com.quectel.core.module.dept.entity.DeptPersonEntity;
import com.quectel.core.module.scheduling.dao.SchedulingAttendanceGroupHeadBindingDao;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupDto;
import com.quectel.core.module.scheduling.dto.SchedulingAttendanceGroupHeadBindingDto;
import com.quectel.core.module.scheduling.entity.SchedulingAttendanceGroupEntity;
import com.quectel.core.module.scheduling.entity.SchedulingAttendanceGroupHeadBindingEntity;
import com.quectel.core.module.scheduling.service.SchedulingAttendanceGroupHeadBindingService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Function;
/**
 *
 * @author zhangheng
 * @email nolan.zhang@quectel.com
 * @date 2023/07/05 13:24
 */
@DubboService
public class SchedulingAttendanceGroupHeadBindingServiceImpl implements SchedulingAttendanceGroupHeadBindingService{

    private static final Logger LOGGER = LoggerFactory.getLogger(SchedulingAttendanceGroupHeadBindingServiceImpl.class);

    /**
     * 入参解释:实体id
     */
    private static final Function<Long, String> SCHEDULING_ATTENDANCE_GROUP_HEAD_BINDING_CACHE_BY_ID_FUNC = id -> String.format(RedisCacheConstants.PROJECT_NAME + "SCHEDULING_ATTENDANCE_GROUP_HEAD_BINDING_CACHE_BY_ID:%d", id);

    /**
     * 清除缓存方法
     */
    private static final Consumer<SchedulingAttendanceGroupHeadBindingDto> CLEAR_CACHE_FUNC = dto -> {
        CacheKit.invalidRedisCache(SCHEDULING_ATTENDANCE_GROUP_HEAD_BINDING_CACHE_BY_ID_FUNC.apply(dto.getId()));
    };

    @Autowired
    private SchedulingAttendanceGroupHeadBindingDao schedulingAttendanceGroupHeadBindingDao;


    @Override
    public List<SchedulingAttendanceGroupDto> selectGroupByPersonId(Long personId) {
        List<SchedulingAttendanceGroupEntity> entities = schedulingAttendanceGroupHeadBindingDao.selectGroupByPersonId(personId);
        return CopyUtils.copyList(entities, SchedulingAttendanceGroupDto.class);
    }

    @Override
    public List<DeptPersonDto> selectPersonByGroupId(Long groupId) {
        List<DeptPersonEntity> entities = schedulingAttendanceGroupHeadBindingDao.selectPersonByGroupId(groupId);
        return CopyUtils.copyList(entities, DeptPersonDto.class);
    }

    /**
     * 根据groupId 查询List
     *
     * @param id
     * @return
     */
    @Override
    public List<SchedulingAttendanceGroupHeadBindingDto> selectListByGroupId(Long id) {
        List<SchedulingAttendanceGroupHeadBindingEntity> entityList = schedulingAttendanceGroupHeadBindingDao.selectList(
                new LambdaQueryWrapper<SchedulingAttendanceGroupHeadBindingEntity>()
                        .eq(SchedulingAttendanceGroupHeadBindingEntity::getSchedulingAttendanceGroupId, id)
        );
        return CopyUtils.copyList(entityList, SchedulingAttendanceGroupHeadBindingDto.class);
    }

    @Override
    public SchedulingAttendanceGroupHeadBindingDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                SCHEDULING_ATTENDANCE_GROUP_HEAD_BINDING_CACHE_BY_ID_FUNC.apply(id),
                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public SchedulingAttendanceGroupHeadBindingDto selectById(Long id){
            SchedulingAttendanceGroupHeadBindingEntity entity = schedulingAttendanceGroupHeadBindingDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity,SchedulingAttendanceGroupHeadBindingDto.class));
    }

    @Override
    public List<SchedulingAttendanceGroupHeadBindingDto> queryList(Map<String,Object> params){
        List<SchedulingAttendanceGroupHeadBindingEntity> list = schedulingAttendanceGroupHeadBindingDao.queryList(params);
        List<SchedulingAttendanceGroupHeadBindingDto> result =  CopyUtils.copyList(list,SchedulingAttendanceGroupHeadBindingDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    @Override
    public List<SchedulingAttendanceGroupHeadBindingDto> queryListWithoutPadding(Map<String,Object> params){
        List<SchedulingAttendanceGroupHeadBindingEntity> list = schedulingAttendanceGroupHeadBindingDao.queryList(params);
        return CopyUtils.copyList(list,SchedulingAttendanceGroupHeadBindingDto.class);
    }

    /**
     * 此方法慎用 禁止填充List一类 比如一个人有多个地址这里不允许填充
     * 填充的原则是: 1:被填充对象不经常变换 2:根据id可以走缓存 3数据足够简单 比如设备类型 所属园区等
     *
     * @param dto
     * @return
     */
    private SchedulingAttendanceGroupHeadBindingDto paddingField(SchedulingAttendanceGroupHeadBindingDto dto){
        if (dto != null){

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params){
        return schedulingAttendanceGroupHeadBindingDao.queryTotal(params);
    }

    @Override
    public Long save(SchedulingAttendanceGroupHeadBindingDto dto){
        SchedulingAttendanceGroupHeadBindingEntity entity = CopyUtils.copyObj(dto,SchedulingAttendanceGroupHeadBindingEntity.class);
        entity.setId(Snowflake.nextId());
        schedulingAttendanceGroupHeadBindingDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(SchedulingAttendanceGroupHeadBindingDto dto){
        SchedulingAttendanceGroupHeadBindingEntity entity = CopyUtils.copyObj(dto,SchedulingAttendanceGroupHeadBindingEntity.class);

        schedulingAttendanceGroupHeadBindingDao.updateById(entity);

        CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void updateAllColumnById(SchedulingAttendanceGroupHeadBindingDto dto) {
       SchedulingAttendanceGroupHeadBindingEntity entity = CopyUtils.copyObj(dto,SchedulingAttendanceGroupHeadBindingEntity.class);

       MyBatisPlusUtils.updateAllColumnById(entity, schedulingAttendanceGroupHeadBindingDao);

       CLEAR_CACHE_FUNC.accept(dto);
    }

    @Override
    public void deleteById(Long id){

        CLEAR_CACHE_FUNC.accept(selectById(id));

        schedulingAttendanceGroupHeadBindingDao.deleteById(id);

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids){
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
